#include <QPalette>
#include <QBrush>

#include "mainWin.h"
#include "globalValue.h"

mainWin::mainWin(QWidget *parent) : QWidget(parent)
{

    this->setWindowFlags(Qt::FramelessWindowHint);//去掉标题栏
    this->setFixedWidth(700);
    this->setFixedHeight(550);

    musicList = new musicListView();
    musicAdapter = new musicListAdapter();
    lastMusic = new buttonNext();
    nextMusic = new buttonNext();
    playButton = new buttonPlay();
    musicProgress = new sliderProgress();
    sound = new soundControl();
    title = new titleBar();
    allTime = new QLabel();
    currentTime = new QLabel();
    //infoWidget = new QWidget();
    controlBarWidget = new QWidget();
    mlcontrol = new musicListControl();
    info = new musicInfo();

    currentMusic = NULL;
    isMusicSeted = 0;
    state = STOP;
    volume = 100;
    isMute = 0;


    musicProgress->setValue(0);
    //musicProgress->setRange(0, 0);



    QPalette palette;
    palette.setBrush(QPalette::Window, QBrush(QPixmap(":/test/img/test.jpg")));


    this->setAutoFillBackground(true);
    this->setPalette(palette);


    controlBar = new QHBoxLayout();
    showArea = new QHBoxLayout();
    mainLayout = new QVBoxLayout();
    infoArea = new QVBoxLayout();
    musicListLayout = new QVBoxLayout();

    musicList->setMusicAdapter(musicAdapter);

    lastMusic->setDire(Last);

    musicList->setFixedWidth(200);

    QObject::connect(title, SIGNAL(moveWindows(QMouseEvent*)),
                     this, SLOT(moveWindows(QMouseEvent*)));

    QObject::connect(title, SIGNAL(titlePressed(QMouseEvent*)),
                     this, SLOT(titlePressed(QMouseEvent*)));
    QObject::connect(title, SIGNAL(closeWindows()), this, SLOT(close()));
    QObject::connect(mlcontrol, SIGNAL(musicAddnew(QUrl)), musicAdapter, SLOT(musicAddnew(QUrl)));
    QObject::connect(musicList, SIGNAL(sendPlayMusic(musicData*)),
                     this, SLOT(getPlayMusic(musicData*)));
    QObject::connect(musicList, SIGNAL(removeMusic(musicData*)),
                     this, SLOT(removeMusic(musicData*)));

    //QObject::connect(this)
    QObject::connect(playButton, SIGNAL(stateChanged(int)),
                     this, SLOT(playButtonStateChanged(int)));
    QObject::connect(musicProgress, SIGNAL(rangeChange(int,int)),
                     this, SLOT(progressRangeChanged(int,int)));
    QObject::connect(musicProgress, SIGNAL(valueChangedTracking(int)),
                     this, SLOT(progressValueChanged(int)));
    QObject::connect(sound, SIGNAL(volumeChange(int)),
                     this, SLOT(volumeChanged(int)));
    QObject::connect(sound, SIGNAL(muteChanged(int)),
                     this, SLOT(muteChanged(int)));
    QObject::connect(lastMusic, SIGNAL(getNext(int)),
                     this, SLOT(getNext(int)));
    QObject::connect(nextMusic, SIGNAL(getNext(int)),
                     this, SLOT(getNext(int)));
    QObject::connect(title, SIGNAL(miniWindows()), this, SLOT(minWindow()));


    QPalette palette2;
    palette2.setColor(QPalette::Window, QColor(0, 0, 0, 0));
    palette2.setColor(QPalette::WindowText, QColor(0xFF, 0xFF, 0xFF, 0xFF));
    allTime->setPalette(palette2);
    currentTime->setPalette(palette2);

    allTime->setFixedSize(50, 50);
    currentTime->setFixedSize(50, 50);


    palette2.setColor(QPalette::Window, widgetBack);
    controlBarWidget->setAutoFillBackground(true);
    controlBarWidget->setPalette(palette2);

    controlBar->setMargin(0);
    controlBar->setSpacing(2);
    controlBar->addWidget(lastMusic);
    controlBar->addWidget(playButton);
    controlBar->addWidget(nextMusic);
    controlBar->addWidget(currentTime);
    controlBar->addWidget(musicProgress);
    controlBar->addWidget(allTime);
    controlBar->addWidget(sound);

    controlBarWidget->setLayout(controlBar);

    infoArea->setMargin(0);
    infoArea->setSpacing(0);
    infoArea->addWidget(title);
    infoArea->addWidget(info);
    //infoArea->add

    musicListLayout->setMargin(0);
    musicListLayout->setSpacing(0);
    musicListLayout->addWidget(mlcontrol);
    musicListLayout->addWidget(musicList);

    showArea->setMargin(0);
    showArea->setSpacing(0);
    //showArea->addWidget(musicList);
    showArea->addLayout(musicListLayout);
    showArea->addLayout(infoArea);

    mainLayout->setMargin(0);
    mainLayout->setSpacing(0);
    mainLayout->addLayout(showArea);
    //mainLayout->addLayout(controlBar);
    mainLayout->addWidget(controlBarWidget);

    this->setLayout(mainLayout);

}


void mainWin::moveWindows(QMouseEvent *event)
{
    //this->move(event);
    this->move(event->globalPos()-lastPoint+this->pos());
    lastPoint = event->globalPos();
}

void mainWin::titlePressed(QMouseEvent *event)
{
    lastPoint = event->globalPos();
}


void mainWin::getPlayMusic(musicData *data)
{
    if(isMusicSeted == 1 && state != STOP)
    {
        stopCurrentMusic();
    }
    isMusicSeted = 1;
    currentMusic = data;
    if(data->handle == NULL)
    {
        data->handle = new musicHandle;
        currentHandle = data->handle;
    }
    else
    {
        currentHandle = data->handle;
    }

    currentHandle->setMusic(data->path.toUtf8().data());
    currentHandle->musicProcess();
    currentHandle->musicPlayThreadStart();
    connect(currentHandle->musicProcessDowork, SIGNAL(processFinished()),
            this, SLOT(processFinished()));
    connect(currentHandle->musicPlayDowork, SIGNAL(statePause()),
            this, SLOT(musicPause()));
    state = PLAY;
    playButton->setState(PLAY);
    info->setHandle(currentHandle);

    if(isMute == 0)
    {
        currentHandle->pcmOutput->setVolume(volume / 100.0);
    }
    else
    {
        currentHandle->pcmOutput->setVolume(0);
    }
}

void mainWin::stopCurrentMusic()
{
    if(isMusicSeted == 1)
    {
        info->stopHandle();
        currentHandle->musicDestroy();
        delete currentHandle;
        isMusicSeted = 0;
        currentMusic->handle = NULL;
        currentMusic = NULL;
        currentHandle = NULL;
        state = STOP;
        playButton->setState(PAUSE);
    }
}


void mainWin::processFinished()
{
    currentHandle->musicStart();
    musicProgress->setRange(0, currentHandle->sumTimeSec);
    connect(currentHandle, SIGNAL(sendCurrentTime(int)),
            musicProgress, SLOT(setValue(int)));
    QObject::connect(musicProgress, SIGNAL(valueChanged(int)),
                     currentHandle, SLOT(musicSetTimeSec(int)));
    //state = PLAY;
}


void mainWin::removeMusic(musicData *d)
{
    if(d == this->currentMusic)
    {
        stopCurrentMusic();
    }
}

void mainWin::playButtonStateChanged(int state)
{
    if(this->state == STOP)
    {
        playButton->setState(PAUSE);
        return;
    }
    if(this->state == state)
    {
        return;
    }
    else
    {
        this->state = state;
        if(state == PAUSE)
        {
            this->musicPause();
        }
        else if(state == PLAY)
        {
            this->musicPlay();
        }
    }
}


void mainWin::musicPlay()
{
    if(this->state == STOP)
    {
        return;
    }

    this->state = PLAY;
    playButton->setState(state);
    currentHandle->musicStart();
}


void mainWin::musicPause()
{
    if(this->state == STOP)
    {
        return;
    }

    this->state = PAUSE;
    playButton->setState(PAUSE);
    currentHandle->musicPause();
}


void mainWin::progressRangeChanged(int min, int max)
{
    int m = max / 60;
    int s = max % 60;
    char tmp[6];
    memset(tmp, 0, 6);
    sprintf(tmp, "%02d:%02d", m, s);
    allTime->setText(tmp);
}


void mainWin::progressValueChanged(int v)
{
    int m = v / 60;
    int s = v % 60;
    char tmp[6];
    memset(tmp, 0, 6);
    sprintf(tmp, "%02d:%02d", m, s);
    currentTime->setText(tmp);
}


void mainWin::volumeChanged(int v)
{
    volume = v;
    if(isMusicSeted == 1 && isMute == 0)
    {
        currentHandle->pcmOutput->setVolume(volume / 100.0);
    }
}


void mainWin::muteChanged(int s)
{
    isMute = s;
    if(isMusicSeted == 1)
    {
        if(isMute == 0)
        {
            currentHandle->pcmOutput->setVolume(volume / 100.0);
        }
        else
        {
            currentHandle->pcmOutput->setVolume(0);
        }
    }
}


void mainWin::getNext(int s)
{
    if(isMusicSeted == 0)
    {
        return;
    }
    musicData* tmp;
    if(s == NEXT)
    {
        tmp = musicAdapter->getNext(currentMusic);
        if(tmp != NULL)
        {
            getPlayMusic(tmp);
            return;
        }
    }
    else if(s == PRE)
    {
        tmp = musicAdapter->getPre(currentMusic);
        if(tmp != NULL)
        {
            getPlayMusic(tmp);
            return;
        }
    }
}


void mainWin::minWindow()
{
    this->setWindowState(Qt::WindowMinimized);
}
